package com.frxuexi.user.view

import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.user.intent.AskIntent
import com.frxuexi.user.viewmodel.AskViewModel
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

/**
 * 请假申请页面
 */
@Composable
fun AskForPage(navHostController: NavHostController,vm:AskViewModel= hiltViewModel()) {

    val kv = MMKV.defaultMMKV()
    val uid = kv.decodeInt("uid", 0)
    var uname by remember {
        mutableStateOf(kv.decodeString("uname", "未登录").toString())
    }

    var role by remember {
        mutableStateOf(kv.decodeInt("role", 0))
    }

    var type by remember {
        mutableStateOf("事假")
    }

    val time = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis())
    var start by remember {
        mutableStateOf(time)
    }
    var end by remember {
        mutableStateOf(time)
    }

    var reason by remember {
        mutableStateOf("")
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is IUiState.onSuccess<*> ->{
                        Log.i("===",it.data.toString())
                        ToastUtils.showLong("申请成功")
                        navHostController.popBackStack()
                    }
                    is IUiState.onError->{
                        Log.i("===",it.msg)
                    }

                    else -> {}
                }
            }
        }
    })

    Column {
        MyTitleBar(navHostController = navHostController, title = "请假申请")
        Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {

            Card(
                colors = CardDefaults.cardColors(containerColor = Color.White),
                border = BorderStroke(1.dp, Color.LightGray)
            ) {
                Row(
                    Modifier
                        .fillMaxWidth()
                        .padding(20.dp)
                ) {
                    AsyncImage(
                        model = "https://img0.baidu.com/it/u=3465811579,2310782493&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1727629200&t=a4a813928f902aff74268863728d556b",
                        contentDescription = "",
                        modifier = Modifier
                            .size(80.dp)
                            .clip(CircleShape),
                        contentScale = ContentScale.Crop
                    )
                    Spacer(modifier = Modifier.width(10.dp))
                    Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                        Text(text = uname)
                        Text(text = if (role == 1) "经理" else if (role == 2) "员工" else "")
                    }
                }
            }

            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "请假类型：")
                RadioButton(selected = type == "事假", onClick = {
                    type = "事假"
                })
                Text(text = "事假")
                RadioButton(selected = type == "病假", onClick = {
                    type = "病假"
                })
                Text(text = "病假")
            }
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "开始时间：")
                OutlinedTextField(modifier = Modifier.fillMaxWidth(),value = start, onValueChange = { start = it })
            }
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "结束时间：")
                OutlinedTextField(modifier = Modifier.fillMaxWidth(),value = end, onValueChange = { end = it })
            }

            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "请假原因：")
                OutlinedTextField(modifier = Modifier.fillMaxWidth(),value = reason, onValueChange = { reason = it })
            }

            Button(modifier = Modifier.fillMaxWidth(), onClick = {
                val time = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
                val leave = AskLeave(
                    null,
                    uid,
                    uname,
                    type,
                    reason,
                    start,
                    end,
                    time,
                    "已提交",
                    ""
                )
                vm.sendIntent(AskIntent.askFor(leave))
            }) {
                Text(text = "提交")
            }
        }
    }
}